﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;
using System.Data;

namespace Accountability4net.MySQLDbProvider
{
    public class MySQLDbAction: BaseAction
    {
        private long id_;
        MySQLDbProvider provider_;

        public MySQLDbAction(MySQLDbProvider provider, ActionType type, IAccount account, decimal amount)
            : base(type, account, amount)
        {
            provider_ = provider;
        }

        public static MySQLDbAction Create(MySQLDbProvider provider, ActionType type, IAccount account, decimal amount)
        {
            var action = new MySQLDbAction(provider, type, account, amount);
            string sql = "INSERT INTO `action` (`type`, `account_id`, `amount`) VALUES (@type, @accountId, @amount)";
            using (var command = new MySqlCommand(sql, provider.Connection))
            {
                MySqlParameter typeParameter = new MySqlParameter("@type", type.ToString());
                command.Parameters.Add(typeParameter);
                MySqlParameter accountIdParameter = new MySqlParameter("@accountId", account.Number);
                command.Parameters.Add(accountIdParameter);
                MySqlParameter amountParameter = new MySqlParameter("@amount", amount);
                amountParameter.DbType = DbType.Decimal;
                command.Parameters.Add(amountParameter);
                int rows = command.ExecuteNonQuery();
                action.id_ = command.LastInsertedId;
            }
            return action;
        }

        public long Id
        {
            get
            {
                return id_;
            }
        }

        public static IEnumerable<IAction> Retrieve(MySQLDbProvider provider, IAccount account, ITransaction transaction)
        {
            string sql = "SELECT * FROM `action` WHERE `account_id` = @accountId AND `transaction_id` = @transactionId";
            using (var command = new MySqlCommand(sql, provider.Connection))
            {
                MySqlParameter accountIdParameter = new MySqlParameter("@accountId", account.Number);
                command.Parameters.Add(accountIdParameter);
                MySqlParameter transactionIdParameter = new MySqlParameter("@transactionId", transaction.ID);
                command.Parameters.Add(transactionIdParameter);
                using (var reader = command.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        var typeString = reader.GetString("type");
                        var type = (ActionType)Enum.Parse(typeof(ActionType), typeString);
                        var amount = reader.GetDecimal("amount");
                        var action = new MySQLDbAction(provider, type, account, amount);
                        action.id_ = reader.GetInt32("id");
                        yield return action;
                    }
                }
            }
        }
    }
}
